﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace XHFrame
{
    /// <summary>
    /// 消息内容
    /// </summary>
    public class MessageCenter : Singleton<MessageCenter>
    {
        /// <summary>
        /// 消息事件委托
        /// </summary>
        private Dictionary<string, List<MessageEvent>> dicMessageEvents = null;

        public override void Init()
        {
            base.Init();
            dicMessageEvents = new Dictionary<string, List<MessageEvent>>();
        }

        #region Add & Remove listener (添加听众与删除监听)
        /// <summary>
        /// 添加监听对象
        /// </summary>
        /// <param name="messageName">通过类型进行添加</param>
        /// <param name="messageEvent">消息事件委托</param>
        public void AddListener(MessageType messageName, MessageEvent messageEvent)
        {
            AddListener(messageName.ToString(), messageEvent);
        }
        /// <summary>
        /// 添加监听对象
        /// </summary>
        /// <param name="messageName">字符字符串的形式添加监听对象</param>
        /// <param name="messageEvent">消息事件委托</param>
        public void AddListener(string messageName, MessageEvent messageEvent)
        {
            List<MessageEvent> list = null;
            if (dicMessageEvents.ContainsKey(messageName))
            {
                list = dicMessageEvents[messageName];
            }
            else
            {
                list = new List<MessageEvent>();
                dicMessageEvents.Add(messageName, list);
            }
            if (!list.Contains(messageEvent))
            {
                list.Add(messageEvent);
            }
        }

        /// <summary>
        /// 移除监听
        /// </summary>
        /// <param name="messageName">通过类型移除监听对象</param>
        /// <param name="messageEvent">消息事件委托</param>
        public void RemoveListener(MessageType messageName, MessageEvent messageEvent)
        {
            RemoveListener(messageName.ToString(), messageEvent);
        }

        /// <summary>
        /// 移除监听
        /// </summary>
        /// <param name="messageName">通过字符串移除监听对象</param>
        /// <param name="messageEvent">消息事件委托</param>
        public void RemoveListener(string messageName, MessageEvent messageEvent)
        {
            if (dicMessageEvents.ContainsKey(messageName))
            {
                List<MessageEvent> list = dicMessageEvents[messageName];
                if (list.Contains(messageEvent))
                {
                    list.Remove(messageEvent);
                }
                else if (list.Count <= 0)
                {
                    dicMessageEvents.Remove(messageName);
                }
            }
        }

        public void RemoveAllListener()
        {
            dicMessageEvents.Clear();
        }

        #endregion


        #region Send Message (发送消息)


 

        public void SendMessage(Message message)
        {
            DoMessageDispatcher(message);
        }

        public void SendMessage(string name, object sender)
        {
            SendMessage(new Message(name, sender));
        }

        public void SendMessage(string name, object sender, object content)
        {
            SendMessage(new Message(name, sender, content));
        }

        public void SendMessage(string name, object sender, object content, params object[] dicParams)
        {
            SendMessage(new Message(name, sender, content, dicParams));
        }

        private void DoMessageDispatcher(Message message)
        {
            //  Debug.Log("DoMessageDispatcher Name:" + message.Name);
            if (dicMessageEvents == null || !dicMessageEvents.ContainsKey(message.Name))
                return;
            List<MessageEvent> list = dicMessageEvents[message.Name];
            for (int i = 0; i < list.Count; i++)
            {
                MessageEvent messageEvent = list[i];
                if (null != messageEvent)
                {
                    messageEvent(message);
                }
            }
        }

        #endregion

    }
}
